<?php
namespace App\Http\Controllers\WebApi\CenterCompany;

use App\Repositories\Admin\CompanyRepository;
use Illuminate\Http\Request;

class CompanyController extends BaseController
{
    /**
     * 公司信息
     */

    protected $com;

    public function __construct()
    {
        parent::__construct();
        $this->com = new CompanyRepository();
    }

    /**
     * 法人信息获取
     */
    public function getLegalInfo(Request $request)
    {
        //用户登陆判断
        $token = $request->post('token','');
        $uid = $this->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-9);
        }
        //获取公司信息
        $com = $this->com->getWebCenterOneByUid($uid);
        if (!$com) {
            return $this->toJson(-3);
        }
        $imgs = $com->legal_idcard_imgs ? explode('|',$com->legal_idcard_imgs) : '';
        $front = $imgs ? $imgs[0] : '';
        $back = $imgs ? $imgs[1] : '';
        $hand = $imgs ? $imgs[2] : '';
        $dataArr = [
            //用户UID
            'uid' => $uid,
            //法人姓名
            'name' => $com->legal_name,
            //法人性别
            'sex' => $com->legal_sex,
            //手机号码
            'mobile' => $com->legal_mobile,
            //身份证号码
            'idcard' => $com->legal_idcard,
            //身份证正面
            'idcardFront' => $front,
            //身份证反面
            'idcardBack' => $back,
            //身份证正面手持
            'idcardHand' => $hand,
        ];
        return $this->toJson(0,$dataArr);
    }

    /**
     * 法人信息设置
     */
    public function setLegalInfo(Request $request)
    {
        //用户登陆判断
        $token = $request->post('token','');
        $uid = $this->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-9);
        }
        //获取参数
        $name = $request->post('legalName','');
        $sex = $request->post('legalSex','');
        $mobile = $request->post('legalMobile','');
        $idcard = $request->post('legalIdcard','');
        $front = $request->post('legalImgFront','');
        $back = $request->post('legalImgBack','');
        $hand = $request->post('legalImgHand','');
        if (!$name || !$mobile) {
            return $this->toJson(-2);
        }
        $com = $this->com->getWebCenterOneByUid($uid);
        if (!$com) {
            return $this->toJson(-3);
        }
        $imgString = '';
        if ($front || $back || $hand) {
            $imgs = [$front, $back, $hand];
            $imgString = implode('|',$imgs);
        }
        $dataArr = [
            'legal_name' => $name,
            'legal_sex' => $sex,
            'legal_mobile' => $mobile,
            'legal_idcard' => $idcard,
            'legal_idcard_imgs' => $imgString,
            'updated_at' => time(),
        ];
        $resUpdate = $this->com->setWebCenterOneByUid($uid,$dataArr);
        if (!$resUpdate) {
            return $this->toJson(-100);
        }
        return $this->toJson(0);
    }

    /**
     * 工厂设备获取
     */
    public function getEquip(Request $request)
    {
        //用户登陆判断
        $token = $request->post('token','');
        $uid = $this->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-9);
        }
        //获取公司信息
        $com = $this->com->getWebCenterOptionByUid($uid);
        if (!$com) {
            return $this->toJson(-3);
        }
        $equips = $com->equips ? json_decode($com->equips,true)  : '';
        $dataArr = array();
        if (!count($equips)) {
            return $this->toJson(-3);
        }
        foreach ($equips as $equip) {
            $dataArr[] = [
                'type' => $equip[0],
                'brand' => $equip[1],
                'year' => $equip[2],
                'img' => $equip[3],
                'num' => $equip[4],
            ];
        }
        return $this->toJson(0,$dataArr);
    }

    /**
     * 工厂设备设置
     */
    public function setEquip(Request $request)
    {
        //用户登陆判断
        $token = $request->post('token','');
        $uid = $this->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-9);
        }
        //获取参数
        $equips = $request->post('equips','');  //设备数组
        if (!is_array($equips) || !$equips) {
            return $this->toJson(-2);
        }
        $com = $this->com->getWebCenterOptionByUid($uid);
        if (!$com) {
            return $this->toJson(-3);
        }
        $equipArr = array();
        foreach ($equips as $equip) {
            $equipArr[] = [
               $equip['type'], $equip['brand'], $equip['year'], $equip['img'], $equip['num'],
            ];
        }
        $dataArr = [
            'equips' => json_encode($equipArr,JSON_UNESCAPED_UNICODE),
            'updated_at' => time(),
        ];
        $resUpdate = $this->com->setWebCenterOptionByUid($uid,$dataArr);
        if (!$resUpdate) {
            return $this->toJson(-100);
        }
        return $this->toJson(0);
    }
}